package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Event9zwz;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("Event9zwzController")
public class Event9zwzController extends CmiEmaController {

	@RequestMapping("/listEvent9zwzs")
	public ModelAndView listEvent9zwzs(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = event9zwzPaginationService.updatePagination(request, pagination);
		Set<Event9zwz> event9zwzs = event9zwzPaginationService.getPaginationEvent9zwzs(pagination);

		mav.addObject("event9zwzs", event9zwzs);
		mav.addObject("entityName", "event9zwz");
		mav.addObject("pagination", pagination);
		mav.setViewName("event9zwz/listEvent9zwzs");
		return mav;
	}

	public String listEvent9zwzs() {
		return "redirect:/listEvent9zwzs";
	}

	@RequestMapping("/viewEvent9zwz")
	public ModelAndView viewEvent9zwz(@RequestParam Integer event9zwzId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("event9zwz", event9zwzDAO.findEvent9zwzByPrimaryKey(event9zwzId));
		mav.setViewName("event9zwz/viewEvent9zwz");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteEvent9zwzs")
	public ModelAndView confirmDeleteEvent9zwzs(
			@RequestParam(value = "deleteEvent9zwzsIds", required = true) Set<Integer> deleteEvent9zwzsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Event9zwz> deleteEvent9zwzs = event9zwzDAO.findEvent9zwzsByIds(deleteEvent9zwzsIds);
		Set<Event9zwz> referencedEvent9zwzs = event9zwzService.loadReferenceEvent9zwzs(deleteEvent9zwzs);
		boolean hasReferenced;
		if (referencedEvent9zwzs.size() == 0) {
			mav.addObject("deleteEntitys", deleteEvent9zwzs);
			hasReferenced = false;
		} else {
			deleteEvent9zwzs.removeAll(referencedEvent9zwzs);
			mav.addObject("referencedEntitys", referencedEvent9zwzs);
			mav.addObject("deleteEntitys", deleteEvent9zwzs);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Event9zwz");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listEvent9zwzs");
		return mav;
	}

	@RequestMapping("/deleteEvent9zwz")
	public String deleteEvent9zwz(@RequestParam Integer event9zwzId) {
		Event9zwz event9zwz = event9zwzDAO.findEvent9zwzByPrimaryKey(event9zwzId);
		event9zwzService.deleteEvent9zwz(event9zwz);
		return "forward:/listEvent9zwzs";
	}

	@RequestMapping("/confirmDeleteEvent9zwz")
	public ModelAndView confirmDeleteEvent9zwz(@RequestParam Integer event9zwzId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("event9zwz", event9zwzDAO.findEvent9zwzByPrimaryKey(event9zwzId));

		mav.setViewName("event9zwz/deleteEvent9zwz");

		return mav;
	}

	@RequestMapping("/deleteEvent9zwzs")
	public String deleteEvent9zwzs(
			@RequestParam(value = "deleteEvent9zwzsId", required = true) Set<Integer> deleteEvent9zwzsId) {
		event9zwzService.deleteEvent9zwzs(deleteEvent9zwzsId);
		return "forward:/listEvent9zwzs";
	}

	@RequestMapping("/editEvent9zwz")
	public ModelAndView editEvent9zwz(@RequestParam Integer event9zwzId) {

		ModelAndView mav = new ModelAndView();

		Event9zwz event9zwz = event9zwzDAO.findEvent9zwzByPrimaryKey(event9zwzId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("event9zwz", event9zwz);

		// 把所有的引用event9zwz对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(event9zwzService.loadEvent9zwzObjectReferencedSet(event9zwz));

		// 将编辑页面event9zwz添加到mav中
		mav.setViewName("event9zwz/editEvent9zwz");
		return mav;
	}

	@RequestMapping("/newEvent9zwz")
	public ModelAndView newEvent9zwz() {
		ModelAndView mav = new ModelAndView();
		Event9zwz event9zwz = new Event9zwz();

		mav.addObject("event9zwz", event9zwz);

		// 把所有的引用event9zwz对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(event9zwzService.loadEvent9zwzObjectReferencedSet(event9zwz));

		mav.addObject("newFlag", true);
		mav.setViewName("event9zwz/editEvent9zwz");

		return mav;
	}

	@RequestMapping("/saveEvent9zwz")
	public ModelAndView saveEvent9zwz(@ModelAttribute Event9zwz event9zwz) {

		ModelAndView mav = new ModelAndView();

		Event9zwz existingEvent9zwz = CmiHibernateProxyUtil
				.deproxy(event9zwzDAO.findEvent9zwzByPhysicalPrimaryKey(event9zwz.getNumber()));

		// 新建记录
		if (event9zwz.getId() == null) {
			if (event9zwz.equals(existingEvent9zwz)) {

				mav.addAllObjects(event9zwzService.loadEvent9zwzClassReferencedSet(event9zwz));
				// mav = editContinue(mav, event9zwz);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("event9zwz/editEvent9zwz");
			} else {
				event9zwzService.saveEvent9zwz(event9zwz);
				mav.setViewName("forward:/listEvent9zwzs");
			}
		}
		// 编辑记录
		else {
			Event9zwz originEvent9zwz = CmiHibernateProxyUtil
					.deproxy(event9zwzDAO.findEvent9zwzByPrimaryKey(event9zwz.getId()));
			if (!event9zwz.equals(originEvent9zwz) && event9zwz.equals(existingEvent9zwz)) {
				mav.addAllObjects(event9zwzService.loadEvent9zwzClassReferencedSet(event9zwz));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("event9zwz/editEvent9zwz");
			} else {
				event9zwzService.saveEvent9zwz(event9zwz);
				mav.setViewName("forward:/listEvent9zwzs");
			}
		}
		return mav;
	}

	@RequestMapping("/event9zwzController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
